package com.wr.springbootadmin.utils;
import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;


/**
 * @author 王瑞
 * @description: 服务器主机名校验类
 * @date 2021/4/20  14:33
 */
public class NullHostNameVerifier implements HostnameVerifier{

    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }



    public static void main(String[] args) throws Exception {
        //设置可通过ip地址访问https请求
        HttpsURLConnection.setDefaultHostnameVerifier(new NullHostNameVerifier());
        TrustManager[] tm = {new MyX509TrustManager()};
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();
        String urlStr = "https://61.164.105.28:6443/image/pic?=d31icc7e*56ei343-a19611e8=t1i4m*=p2p0i=d1s*i2d1=*1b1i7d4e*7b5bde7d8-bc8e080-1181s9-81z6f1d8=48";
        URL url = new URL(urlStr);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setSSLSocketFactory(ssf);
        con.setRequestMethod("POST"); // 设置以POST方式提交数据
        con.setDoInput(true); // 打开输入流，以便从服务器获取数据
        con.setDoOutput(true);// 打开输出流，以便向服务器提交数据
        //设置发送参数
        String param = "sfmc=测试";
        PrintWriter out = new PrintWriter(new OutputStreamWriter(con.getOutputStream(),"UTF-8"));
        out.print(param);
        out.flush();
        out.close();
        //读取请求返回值
        InputStreamReader in = new InputStreamReader(con.getInputStream(),"UTF-8");
        BufferedReader bfreader = new BufferedReader(in);
        String result = "";
        String line = "";
        while ((line = bfreader.readLine()) != null) {
            result += line;
        }
        System.out.println("result:"+result);
    }
}
